using UnityEngine;
using System.Collections;

public class AchievementManager : MonoBehaviour
{

    
	public AchieveListItem[] alts;
	
	public GameObject achieveItem;	
	
	public Transform back;
	
	public AchievementItem[] achieveItems;
	
	public bool AchievimentClicked = false;
	public int achievementClicked = -1;

    public newMenu menu;


	public int Distancia = 100;
	private int currentAchieve = 0;
	
	public static AchievementManager getInstance;
	
	private bool canNavigate = false;
	
	
	private Vector3 backPosition;
	
	public GameObject backObject;
	
	public GameObject arrow;
	
	private Transform arrowManager;

    public GameObject father;

    void Awake()
    {
        father = new GameObject();
        getInstance = this;
    }

	IEnumerator Start ()
	{
		// Por depois de acrescentar o botão de ir para tela de Achievements;

        yield return new WaitForSeconds(0.2f);
        this.achieveItems = AchieveStorage.AchievementItems;
		
		
		alts = new AchieveListItem[achieveItems.Length];
		
		for (int i = 0; i < achieveItems.Length; i++) {
			GameObject go = Instantiate(achieveItem, new Vector3(achieveItem.transform.position.x,achieveItem.transform.position.y - i*Distancia , achieveItem.transform.position.z), achieveItem.transform.rotation) as GameObject;
            AchieveListItem alt = go.GetComponent<AchieveListItem>();

			alt.title.GetComponent<UI_TextMessage>().id = achieveItems[i].titleId;
			alt.description.GetComponent<UI_TextMessage>().id = achieveItems[i].subtitleId;
			
			alt.activeGetText();
			
			alt.SetUnlocked(achieveItems[i].completed);
			alt.SetId(i);
			#if UNITY_STANDALONE_WIN || UNITY_WEBPLAYER
			if(i == 0)
			{
                
				Instantiate(arrow,new Vector3(arrow.transform.position.x,achieveItem.transform.position.y - i*Distancia + 0.5f , arrow.transform.position.z),arrow.transform.rotation);
				arrowManager = GameObject.FindGameObjectWithTag("Arrow").transform;
                arrowManager.parent = father.transform;
			}
			#endif
			
			if(i == achieveItems.Length - 1)
			{
				
				ScrollPlane.yMin = achieveItem.transform.position.y - i*Distancia;
				backPosition = new Vector3(achieveItem.transform.position.x,achieveItem.transform.position.y - i*Distancia - 3 , achieveItem.transform.position.z);

			}

			alts[i] = alt;
            if(i == 0)
            Camera.mainCamera.transform.position = new Vector3(alts[0].transform.position.x, alts[0].transform.position.y, Camera.mainCamera.transform.position.z);

            go.transform.parent = father.transform;
		}
			
		Instantiate(back, backPosition , back.rotation);
		
		backObject = GameObject.FindGameObjectWithTag("backButton");
        backObject.transform.parent = father.transform;

		canNavigate = true;

        father.transform.parent = gameObject.transform;
	}

	void Update ()
	{
		#if UNITY_STANDALONE_WIN || UNITY_WEBPLAYER
		if(Input.GetAxisRaw("Vertical") == -1 && canNavigate)
		{

			if(currentAchieve + 1 == alts.Length)
			{
				if(!backObject.GetComponent<SpecialCube>())
				backObject.AddComponent("SpecialCube");	
				currentAchieve++;
			}
			
			if(currentAchieve + 1 < alts.Length){
			currentAchieve++;

            Camera.mainCamera.transform.position = new Vector3(alts[currentAchieve].transform.position.x, alts[currentAchieve].transform.position.y, Camera.mainCamera.transform.position.z);
			arrowManager.transform.position = new Vector3(arrowManager.position.x,alts[currentAchieve].transform.position.y+0.5f,arrowManager.position.z);
			}
						
			StopAllCoroutines();
			StartCoroutine("returnInput");	
		}
		if(Input.GetAxisRaw("Vertical") == 1 && canNavigate)
		{
			if(currentAchieve == alts.Length)
			{
				if(backObject.GetComponent<SpecialCube>())
				Destroy(backObject.GetComponent<SpecialCube>());
				
				backObject.renderer.material.color = Color.white;
				currentAchieve --;
			}
			else if(currentAchieve - 1 >= 0){
			currentAchieve--;

            Camera.mainCamera.transform.position = new Vector3(alts[currentAchieve].transform.position.x, alts[currentAchieve].transform.position.y, Camera.mainCamera.transform.position.z);
			arrowManager.transform.position = new Vector3(arrowManager.position.x,alts[currentAchieve].transform.position.y+0.5f,arrowManager.position.z);

			}
			
			StopAllCoroutines();
			StartCoroutine("returnInput");
		}
		
		if(Input.GetButtonDown("Jump") || (Input.GetKeyDown(KeyCode.KeypadEnter) ||  Input.GetKeyDown(KeyCode.Return)))
		{
			if(currentAchieve == alts.Length)
			{
                menu.Back();
                Transform player = GameObject.FindGameObjectWithTag("Player").transform;
                Camera.mainCamera.transform.position = new Vector3(player.position.x, player.position.y + 4, Camera.mainCamera.transform.position.z);
			}
		}
		#endif
		
		#if UNITY_IPHONE || UNITY_ANDROID

        if (Input.GetKeyDown(KeyCode.Return))
        {
            menu.Back();
        }

		if(Input.touchCount == 1 && Input.touches[0].phase == TouchPhase.Ended)
		{
			Ray ray = Camera.mainCamera.ScreenPointToRay(new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0f));
			RaycastHit hit;
		
			if (Physics.Raycast(ray, out hit, Mathf.Infinity))
			{
				if(hit.transform.tag == "backButton")
				{
					menu.Back();
                    Transform player = GameObject.FindGameObjectWithTag("Player").transform;
                    Camera.mainCamera.transform.position = new Vector3(player.position.x, player.position.y + 4, Camera.mainCamera.transform.position.z);
			
				}
			}
		}
		
#endif
    }

    public void ClearIndex()
    {
       if (backObject.GetComponent<SpecialCube>())
           Destroy(backObject.GetComponent<SpecialCube>());

            backObject.renderer.material.color = Color.white;
            currentAchieve = 0;
            arrowManager.transform.position = new Vector3(arrowManager.position.x, alts[currentAchieve].transform.position.y + 0.5f, arrowManager.position.z);
            Camera.mainCamera.transform.position = new Vector3(alts[currentAchieve].transform.position.x, alts[currentAchieve].transform.position.y, Camera.mainCamera.transform.position.z);
            
            for (int i = 0; i < alts.Length; i++)
			{
			    Transform t = alts[i].transform;
                
                foreach (Transform currentTransform in t.transform)
	            {
		            if(currentTransform.GetComponent<SpecialCube>())
                        currentTransform.gameObject.SendMessage("OnResume");
	            }
			}

    }

    void ReturnInputCall()
    {
        ClearIndex();
       StartCoroutine("returnInput");
    }


	IEnumerator returnInput()
	{
		canNavigate = false;
		yield return new WaitForSeconds(0.2f);
		canNavigate = true;
	}
	
	public void Open (int id)
	{
		//Camera.mainCamera.transform.position = new Vector3(alts[currentAchieve].transform.position.x, alts[currentAchieve].transform.position.y, alts[currentAchieve].transform.position.z - 14);
		
		foreach(AchieveListItem alt in alts)
		{
			alt.changeColor(false);	
		}
		
		currentAchieve = id;
		alts[currentAchieve].changeColor(true);
		
	}

}
